<?php ob_start();?>
<html>
<?php 
	$root = $_SERVER['DOCUMENT_ROOT'];
 	include($root.'/standart/head.php');
 	include($root.'/standart/dateHeader.php');
 	include($root.'/standart/checkUser.php');
?>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Start Bootstrap - SB Admin Version 2.0 Demo</title>
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link href="../font-awesome/css/font-awesome.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="../css/jquery.cleditor.css" />
    <link href="../css/sb-admin.css" rel="stylesheet">
    <link href="../css/jquery-ui-1.10.4.css" rel="stylesheet" type="text/css" media="screen"/>
	<link href="../css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
    
    <script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="../js/ajax.js"></script>
    <script type="text/javascript">
    function validate(evt) {
		var theEvent = evt || window.event;
	  	var key = theEvent.keyCode || theEvent.which;
	  	key = String.fromCharCode( key );
	  	var regex = /[0-9]|\./;
	  	if( !regex.test(key) ) {
	    	theEvent.returnValue = false;
	    	if(theEvent.preventDefault) theEvent.preventDefault();
	  	}
	}

	var tabldotRow = "<td><input type=\"checkbox\" /></td>"
        +"<td></td>"
        +"<td></td>"
        +"<td>"
       	+"	<select class=\"form-control\" >"
        +"		<option value=\"1\">Çorba</option>"
        +"		<option value=\"2\">Ara Sıcak</option>"
        +"		<option value=\"3\">Ana Yemek</option>"
        +"		<option value=\"4\">İkram</option>"
        +"	</select>"
        +"</td>"
        +"<td style=\"display: none;\"></td>"
        +"<td style=\"display: none;\"></td>";
	
	function onLoad(){
		parent.unloadPageX();
		$('.datepicker').datepicker();
		var table = $('#dataTables-example').dataTable({
            "language": {
                "url": "../css/plugins/dataTables/dataTablesTurkish.json"
            }
        } );
		$('#dataTables-example tbody').on( 'click', 'tr', function () {

			if($("#menuDate").val() == ''
				|| !dateControl($("#menuDate").val())){
				alert("Günün tarihini yada ileriki bir tarih seçiniz!");
				return;
			}

			row = $(this).find("td");
			
			rowOfTabldot = $("#dataTables").find("tr");
			for ( var i = 0; i < rowOfTabldot.length; i++) {
				if(rowOfTabldot[i].cells.length > 2){
					if(rowOfTabldot[i].cells[4].innerText == row[3].innerText){
						return;
					}
				}
			}
	        
			var createdTable = document.getElementById("dataTables");
	        
	        var tempTable = document.createElement('table');
	        tempTable.setAttribute("class","table table-striped table-bordered table-hover");
	        tempTable.id = "dataTables";

			for(var i = 0; i < createdTable.children.length; i++){
				if(createdTable.children[i].tagName == "THEAD"
					|| (createdTable.children[i].tagName == "TBODY" 
						&& createdTable.children[i].children[0].children.length > 1
						)
					|| createdTable.children[i].tagName == "TR"
					|| createdTable.children[i].tagName == "TH"){
						tempTable.appendChild(createdTable.children[i]);
						i--;
				}
			}

			document.getElementById("tabldot_div").innerHTML = "";
			document.getElementById("tabldot_div").appendChild(tempTable);
			
	        var addedRow = document.createElement('tr');
	        addedRow.style.cursor = "pointer";
	        addedRow.innerHTML = tabldotRow;
	        addedRow.cells[1].innerText = row[0].innerText;
	        addedRow.cells[2].innerText = row[1].innerText;
	        addedRow.cells[4].innerText = row[3].innerText;
			if($('#dataTables').find("tbody").length <= 0){
				$('#dataTables').append(document.createElement('tbody'));
			}
	        $('#dataTables tbody').append(addedRow);

	        var table = $('#dataTables').dataTable({
	            "language": {
	                "url": "../css/plugins/dataTables/dataTablesTurkish.json"
	            }
	        } );
	    } );
		var table = $('#dataTables').dataTable({
            "language": {
                "url": "../css/plugins/dataTables/dataTablesTurkish.json"
            }
        } );
        callMethod = "dateSet";
	}

	function getTabldotTable(dateVal){
			url = 'getTabldot.php?'
				+'DATE='+dateVal;
			ajx(url, 'fillTabldot');
	}

	function fillTabldot(_htm){
		document.getElementById("tabldot_div").innerHTML = _htm;
		var table = $('#dataTables').dataTable({
            "language": {
                "url": "../css/plugins/dataTables/dataTablesTurkish.json"
            }
        } );
	}
	
	function dateSet(obj){
		if(obj.value != ''){
			getTabldotTable(obj.value);
			if(dateControl(obj.value)){
				$("#btnDel").prop("disabled", false);
				$("#btnSave").prop("disabled", false);
			}else{
				$("#btnDel").prop("disabled", true);
				$("#btnSave").prop("disabled", true);
			}
		}
	}
	
	function warningMessage() {
		pane = document.getElementById('invisContentView');
		if(pane.style.height == '0px'){
			pane.style.display = 'block';
			$("#invisContentView").animate({
			    height: "30px"
			}, 1000 );
			setTimeout(function(){warningMessage()}, 5000);
		}else{
			pane.style.display = 'block';
			$("#invisContentView").animate({
			    height: "0px"
			}, 1000 , function() {
				document.getElementById('invisContentView').style.display = 'none';
			});
		}
	}

	var mounthToday = parseInt('<?php echo  date('m', time());?>');
	var dayToday = parseInt('<?php echo  date('d', time());?>');
	var yearToday = parseInt('<?php echo  date('Y', time());?>');
	function dateControl(date){
		dates = date.split('/'); 
		day = parseInt(dates[0]);
		mounth = parseInt(dates[1]);
		year = parseInt(dates[2]);
		if(year >= yearToday){
			if(year == yearToday){
				if(mounth >= mounthToday){
					if(mounth == mounthToday){
						if(day >= dayToday){
							return true;
						}else{
							return false;
						}
					}
					return true;
				}else{
					return false;
				}
			}
			return true;
		}else{
			return false;
		}
	}
	var saveCount = 0;
	var errorOccur = false;
	function saveTabldot(){
// 		parent.loadPageX();
		var rows = $('#dataTables tbody').find("tr");
		for(var i = 0; i < rows.length; i++){
			url = 'setTabldot.php?'
				+'DATE='+$("#menuDate").val()
				+'&MENU='+rows[i].cells[4].innerText
				+'&CATEGORY='+rows[i].cells[3].children[0].value;
			if(rows[i].cells[5].innerText != ''){
				url = url + '&ID='+rows[i].cells[5].innerText;
			}
			saveCount++;
			ajx(url, 'saveAll');
		}
	}

	function delTabldot(){
		var rows = $('#dataTables tbody').find("tr");
		var len = rows.length;
		for(var i = 0; i < len; i++){
			if(rows[i].cells[0].children[0].checked){
				if(rows[i].cells[5].innerText != ''){
					url = 'setTabldot.php?DELETE=1'
						+'&ID='+rows[i].cells[5].innerText;
					saveCount++;
					ajx(url, 'delAll');
				}
				tr = rows[i];
				$(tr).fadeOut(600, function(){$(tr).remove();});
				
// 				$('#dataTables tbody').remove(rows[i]);
// 				i--;
// 				len--;
			}
		}
		saveCount++;
		delAll('');
	}
	
	function saveAll(_htm) {
		if(_htm != ''){
			errorOccur = true;
		}
		saveCount--;
		if(saveCount == 0){
			parent.unloadPageX();
			if(errorOccur){
				warningMessage();
				errorOccur = false;
			}
			getTabldotTable($("#menuDate").val());
		}
	}

	function delAll(_htm) {
		if(_htm != ''){
			errorOccur = true;
		}
		saveCount--;
		if(saveCount == 0){
			if(errorOccur){
				warningMessage();
				errorOccur = false;
				getTabldotTable($("#menuDate").val());
			}
		}
	}
    </script>
</head>

<body onload="onLoad()">
    <div id="wrapper">
        <div id="inner-page-wrapper">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">Tabldot Menü Oluşturma</h1>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <div id="invisContentView" style="display: none; height: 0px;">
            	<span style="color:red; font-weight:bolder;">İşlem sırasında hata alındı </span>
            </div>
            <div class="row">
            	<div class="col-lg-6">
            	</div>
            	<div class="col-lg-6">
            		<div class="row">
            			<div class="col-lg-4">
	            			<div class="form-group" >
								<label>Menü Tarihi</label>
		                        <input type="text" class="form-control datepicker" id="menuDate" name="menuDate" >
							</div>	
						</div>
					</div>
            	</div>
            </div>
            <div class="row">
                <div class="col-lg-6">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                        	Yemek Listesi
                        </div>
                        <div class="panel-body">
                        	<?php 
                        	$db = connect();
                        	$stmt ="SELECT PRODUCT_NAME, NOTES, PRICE, MENU.ID AS ID 
									FROM MENU, PRICE_LIST
									WHERE PRICE_LIST.MENU_ID = MENU.ID
									AND APPLICABLE_FROM = (SELECT MAX(APPLICABLE_FROM) from PRICE_LIST where MENU_ID = MENU.ID and APPLICABLE_FROM < CURRENT_TIMESTAMP)";
                        	$res = $db->query($stmt) or die($stmt."<br/><br/>".mysql_error());
                        	?>
                        		<table class="table table-striped table-bordered table-hover" id="dataTables-example">
                        			<thead>
                        	        	<tr>
                        	                <th nowrap="nowrap">Yemek Adı</th>
                        	                <th nowrap="nowrap">Açıklama</th>
                        	                <th nowrap="nowrap">Fiyat</th>
                        	                <th style="display: none;">ID</th>
                        	            </tr>
                        			</thead>
                        	        <tbody>
                        	        <?php $odd = true; ?>
                        	       	<?php while($row = $res->fetch_assoc()){?>
                        	        	<tr class="<?php echo $odd ? "odd" : "even";?> " style="cursor: pointer;">
                        		            <td><?php echo $row["PRODUCT_NAME"];?></td>
                        	                <td><?php echo $row["NOTES"];?></td>
                        	                <td style="text-align: right;"><?php echo $row["PRICE"];?></td>
                        	                <td style="display: none;"><?php echo $row["ID"];?></td>
                        				</tr>
                        				<?php $odd = !$odd;?>
                        			<?php }?>
                        			</tbody>
                        	     </table>
                        		<?php 
                        	$res->close();
                        	disconnect($db);
                        	?>
			            </div>
			       </div>
			   	</div>
			   	<div class="col-lg-6">
			   		<div class="panel panel-default">
                        <div class="panel-heading">
                        	Tabldot Listesi
                        </div>
                        <div class="panel-body" id="tabldot_div">
                        	<table class="table table-striped table-bordered table-hover" id="dataTables">
                        		<thead>
                        	    	<tr>
                        	        	<th nowrap="nowrap">Seç</th>
                        	        	<th nowrap="nowrap">Yemek Adı</th>
                        	            <th nowrap="nowrap">Açıklama</th>
                        	            <th nowrap="nowrap">Kategori</th>
                        	            <th style="display: none;">MENU_ID</th>
                        	            <th style="display: none;">ID</th>
									</tr>
                        		</thead>
								<tbody>
                        		</tbody>
							</table>
			            </div>
			       </div>
			   	</div>
            </div>
            <!-- /.row -->
            <div class="row">
            	<div class="col-lg-6">
            	</div>
            	<div class="col-lg-6">
            		<button type="button" id="btnSave" class="btn btn-default" onclick="saveTabldot()">Kaydet</button>
					<button type="button" id="btnDel" class="btn btn-default" onclick="delTabldot()">Sil</button>
            	</div>
            </div>
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->
    <!-- Page-Level Demo Scripts - Forms - Use for reference -->
</body>
    <script type="text/javascript" src="../js/jquery-ui-1.10.4.js"></script>
    <script type="text/javascript" src="../js/plugins/dataTables/jquery.dataTables.js"></script>
    <script type="text/javascript" src="../js/plugins/dataTables/dataTables.bootstrap.js"></script>
</html>
<?php ob_end_flush(); ?>